import os
import pygame
import random
import time
import threading
import time

pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
pygame.mixer.init()

def play_music(file_path):
	pygame.mixer.music.load(file_path)
	#pygame.mixer.music.play()
	pygame.mixer.music.play(loops=-1, start=0.0)

def pause_music():
	pygame.mixer.music.pause()

def unpause_music():
	pygame.mixer.music.unpause()

def stop_music():
	pygame.mixer.music.stop()

def play_single_song(folder_path):
	song_name = input("Enter the name of the song you want to play (include the file extension): ")
	file_path = os.path.join(folder_path, song_name)
	if os.path.exists(file_path):
		print(f"Playing: {song_name}")
		play_music(file_path)
		while True:
			command = input("Type 'p' to pause, 'r' to resume, 's' to stop, 'l' to toggle loop, "
							"'sh' to toggle shuffle, 'e' to exit: ")
			if command.lower() == 'p':
				pause_music()
			elif command.lower() == 'r':
				unpause_music()
			elif command.lower() == 's':
				stop_music()
				print("Song stopped.")
				break
			elif command.lower() == 'l':
				pygame.mixer.music.set_endevent(pygame.USEREVENT)
				loop = not pygame.mixer.music.get_endevent() # Toggle loop
				pygame.mixer.music.set_endevent(pygame.USEREVENT if loop else None)
				print(f"Loop {'enabled' if loop else 'disabled'}.")
			elif command.lower() == 'sh':
				print("Cannot shuffle in single-song mode.")
			elif command.lower() == 'e':
				stop_music()
				break
			else:
				print("Invalid command.")
	else:
		print(f"Song '{song_name}' not found in the folder.")

def play_all_songs_in_folder(folder_path):
	audio_extensions = ['.mp3', '.wav', '.ogg', '.flac']
	files = [file for file in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, file)) and
			os.path.splitext(file)[1].lower() in audio_extensions]
	current_song_index = 0
	loop = False
	shuffle = False

	while True:
		file_path = os.path.join(folder_path, files[current_song_index])
		print(f"Playing: {files[current_song_index]}")
		play_music(file_path)
		while True:
			for event in pygame.event.get():
				if event.type == pygame.USEREVENT and loop:
					play_music(file_path)
				elif event.type == pygame.USEREVENT and not loop:
					current_song_index = (current_song_index + 1) % len(files)
					file_path = os.path.join(folder_path, files[current_song_index])
					play_music(file_path)
			command = input("Type 'p' to pause, 'r' to resume, 'n' to play the next song, "
							"'b' to play the previous song, 's' to stop, 'l' to toggle loop, "
							"'sh' to toggle shuffle, 'e' to exit: ")
			if command.lower() == 'p':
				pause_music()
			elif command.lower() == 'r':
				unpause_music()
			elif command.lower() == 'n':
				current_song_index = (current_song_index + 1) % len(files)
				break
			elif command.lower() == 'b':
				current_song_index = (current_song_index - 1) % len(files)
				break
			elif command.lower() == 's':
				stop_music()
				print("Song stopped.")
				return
			elif command.lower() == 'l':
				loop = not loop
				pygame.mixer.music.set_endevent(pygame.USEREVENT if loop else None)
				print(f"Loop {'enabled' if loop else 'disabled'}.")
			elif command.lower() == 'sh':
				shuffle = not shuffle
				if shuffle:
					random.shuffle(files)
					print("Playlist shuffled.")
				else:
					files.sort()
					print("Playlist unshuffled.")
			elif command.lower() == 'e':
				stop_music()
				return
			else:
				print("Invalid command.")

def play_the_song():
	file_path = os.path.join("/home/cmh/Music", "才旦卓玛 - 北京的金山上.mp3")
	if os.path.exists(file_path):
		play_music(file_path)
		#StartTime = time.time()
		while True: 
		    pass
		#	StopTime = time.time()
		#	time.sleep(0.0001)
			#TimeElapsed = StopTime - StartTime
			#if TimeElapsed>18:
			#	stop_music()
			#	break
			
PLAY_MUSIC_START=0
PLAY_MUSIC_STOP=1
cmd_play_music=PLAY_MUSIC_STOP
current_cmd_play_music=cmd_play_music

if __name__ == "__main__":
	music_thread = threading.Thread(target=play_the_song)
	music_thread.start()
